/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var PolicyObj = {
    IP_Segments: {
        IP_Segment:[]
    }
};

//获取url参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

function addIPBox() {
    var cont = '';
    cont += "<div class='modal fade' id='myModal' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>";
    cont += "            <div class='modal-dialog'>";
    cont += "                <div class='modal-content'>";
    cont += "                    <div class='modal-header'>";
    cont += "                        <button type='button' class='close' data-dismiss='modal'><span aria-hidden='true'>&times;</span></button>";
    cont += "                        <h4 class='modal-title' id='myModalLabel'>添加IP地址段</h4>";
    cont += "                    </div>";
    cont += "                    <div class='modal-body'>";
    cont += "                        <form class='form-horizontal'>";
    cont += "                            <div class=' row-fluid'>";
    cont += "                                <div class='control-group'>";
    cont += "                                    <label class='control-label' for='IPType'>IP地址类型：</label>";
    cont += "                                    <div class='controls'>";
    cont += "                                        <select id='IPType' class='IPType'><option>IPv4</option><option>IPv6</option></select>";
    cont += "                                    </div>";
    cont += "                                </div>";
    cont += "                                <div class='control-group'>";
    cont += "                                    <label class='control-label' for='IPv4Address'>IP地址：</label>";
    cont += "                                    <div class='controls'>";
    cont += "                                        <input type='text' id='IPv4Address' name='IPv4Address' class='IPv4Address ip' required/>";
    cont += "                                    </div>";
    cont += "                                </div>";
    cont += "                                <div class='control-group'>";
    cont += "                                    <label class='control-label' for='MSrcIPPrefixLen'>IP地址前缀长度：</label>";
    cont += "                                    <div class='controls'>";
    cont += "                                        <input type='text' id='MSrcIPPrefixLen' name='MSrcIPPrefixLen' class='MSrcIPPrefixLen number' required/>";
    cont += "                                    </div>";
    cont += "                                </div>";
    cont += "                            </div>";
    cont += "                        </form>";
    cont += "                    </div>";
    cont += "                    <div class='modal-footer'>";
    cont += "                        <a href='javascript:void(0)' class='btn btn-default' onclick='closeBox()'>取消</a>";
    cont += "                        <a href='javascript:void(0)' class='btn btn-primary' onclick='addIPSeg()'>确定</a>";
    cont += "                    </div>";
    cont += "                </div>";
    cont += "            </div>";
    cont += "        </div>";
    $("body").append(cont);
    $("#myModal").modal('show');
}

function closeBox() {
    $("#myModal").modal('hide')
    $("#myModal").remove();
}

// 刷新IP地址段列表
function showSrc() {
    var obj = $(".MSrcIPSegmentNum tbody");
    var cont = "";
    var number = 0;
    for (var i in PolicyObj.IP_Segments.IP_Segment) {
        cont += "<tr>";
        cont += "<td>" + PolicyObj.IP_Segments.IP_Segment[i].UserIP.IPType + "</td>";
        cont += "<td>" + PolicyObj.IP_Segments.IP_Segment[i].UserIP.IPv4Address + "</td>";
        cont += "<td>" + PolicyObj.IP_Segments.IP_Segment[i].UserIP_Prefix + "</td>";
        cont += "<td><a href='javascript:delSrc(" + number + ")'>&nbsp;删&nbsp;除&nbsp;</a></td>";
        cont += "</tr>";
        number++;
    }
    obj.html("");
    obj.append(cont);
}

function delSrc(num) {
    PolicyObj.IP_Segments.IP_Segment.splice(num, 1);
    showSrc();
}

function addIPSeg() {
    var form = $("#myModal .form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    var IPPrefixLen = $("#MSrcIPPrefixLen").val();
    var IPType = $("#IPType").val();
    var IPv4Address = $("#IPv4Address").val();
    
    var obj = {
        UserIP_Prefix: IPPrefixLen,
        UserIP: {
            IPType: IPType,
            IPv4Address: IPv4Address
        }
    }
    PolicyObj.IP_Segments.IP_Segment.push(obj);
    showSrc();
    closeBox();
}

function generateParameter() {
    PolicyObj.Bind_Action = $("#Band_Action").val();
    PolicyObj.UserName = $("#UserName").val();
    return PolicyObj;
}

//返回上一级
function previous() {
    window.location = "../policyList.html?type=" + type + "&id=" + DepotID + "&name=" + base64.encode(DepotName) + "";
}

function save() {
    var form = $(".main-container .form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    var comment = $(".policy_desc").val();
    var policy = generateParameter();
    policy = JSON.stringify(policy);
    
    $.ajax({
        method:"POST",
        async: false, 
        url: "../../action/policy/create.do",
        data: {comment: comment, policy: policy, type: type, instance: DepotName},
        dataType: "json",
        success: function (data) {
            if (data.result == "true") {
                alert("添加成功！");
                previous();
            } else {
                alert("添加失败!")
            }
        }
    });
}

function edit() {
    var form = $(".main-container .form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    var comment = $(".policy_desc").val();
    var policy = generateParameter();
    policy = JSON.stringify(policy);
    
    $.ajax({
        method:"POST",
        async: false, 
        url: "../../action/policy/update.do",
        data: {messageNo: id, comment: comment, policy: policy, type: type, instance: DepotName},
        dataType: "json",
        success: function (data) {
            if (data.result == "true") {
                alert("添加成功！");
                previous();
            } else {
                alert("添加失败!")
            }
        }
    });
}

function showData(resp) {
    var messageType = resp.MessageType;
    var messageNo = resp.MessageNo;
    var instanceName = resp.Instance;
    var comment = resp.Comment;
    PolicyObj = resp.Content;
    
    $(".policy_desc").val(comment);
    $("#Band_Action").val(PolicyObj.Bind_Action);
    $("#UserName").val(PolicyObj.UserName);
    
    showSrc();
}

var base64 = new Base64();
var type = getQueryString("type");
var DepotID = getQueryString("DepotID");
var DepotName = getQueryString("DepotName");
DepotName = base64.decode(DepotName);

/*
 * 查看旧版本的策略内容
 */
function show_oldPolicy(array, number) {
    var souData = array[number];
    $(".header").text("当前策略在" + souData.CreateTime + "创建,ID为" + souData.MessageNo + ",版本号为" + souData.MessageSerialNo + ",属于" + souData.Instance + "策略库");
    showData(souData);
}

/*
 *获取所有版本的策略内容 
 */
function getAllPolicy() {
    $.ajax({
        type: "GET",
        async: false,
        url: "../../action/policy/history_policy_info.do",
        data: {type: type, instance: getQueryString("DepotName"), messageNo: id},
        dataType: "json",
        success: function(data) {
            policyArray = data;
            policyAmount = data.length
        }
    });
}